package com.example.english.activity;

import android.os.Bundle;
import android.os.ParcelUuid;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.english.App;
import com.example.english.R;
import com.example.english.Utils;
import com.example.english.adapter.PhotogAdapter;
import com.example.english.db.WordBean;

import java.util.ArrayList;
import java.util.List;

public class PhotographActivity extends AppCompatActivity {

    private RecyclerView recyclerView ;
    private PhotogAdapter adapter ;
    private EditText editText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photograph);
        Utils.setBarColor(this, true);
        recyclerView = findViewById(R.id.recyclerView);
        editText = findViewById(R.id.et_search);
        adapter = new PhotogAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        List<WordBean> list = App.dbsqLite.getListWord();
        if(list != null){
            adapter.setList(list);
        }


        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i0, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int pi, int i1, int i2) {
                String s = editText.getText().toString();
                if(TextUtils.isEmpty(s)){
                    List<WordBean> list = App.dbsqLite.getListWord();
                    if(list != null){
                        adapter.setList(list);
                    }
                }
//                else{
//                    List<WordBean> beans = new ArrayList<>();
//                    for (int i = 0 ; i < adapter.getList().size(); i++){
//                        if(adapter.getList().get(i).getEnglish().contains(s) || adapter.getList().get(i).getTranslate().contains(s)){
//                            beans.add(adapter.getList().get(i));
//                        }
//                    }
//                    adapter.setList(beans);
//                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i2, KeyEvent keyEvent) {
                String s = editText.getText().toString();
                List<WordBean> beans = new ArrayList<>();
                for (int i = 0 ; i < adapter.getList().size(); i++){
                    if(adapter.getList().get(i).getEnglish().contains(s) || adapter.getList().get(i).getTranslate().contains(s)){
                        beans.add(adapter.getList().get(i));
                    }
                }
                adapter.setList(beans);
                return true;
            }
        });

    }

    public void onFinish(View view){
        finish();
    }

}
